package ext.tianma.part.util.updateIBAValues;

import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.List;
import wt.fc.PersistenceHelper;
import wt.fc.QueryResult;
import wt.fc.collections.WTArrayList;
import wt.part.WTPart;
import wt.query.KeywordExpression;
import wt.query.QueryException;
import wt.query.QuerySpec;
import wt.query.SearchCondition;
import wt.type.ClientTypedUtility;
import wt.type.TypeDefinitionForeignKey;
import wt.type.TypeDefinitionReference;
import wt.type.Typed;
import wt.util.WTException;
import wt.vc.config.LatestConfigSpec;
import wt.vc.views.View;
import wt.vc.views.ViewHelper;
import wt.vc.wip.WorkInProgressHelper;

/**
 * 用于刷新部件的属性,发送到erp
 * 
 * @author laichao_sun
 *
 */
public class RefreshPartIBAValues {

	public static final String discardState = "OBSOLESCENCE";// 废弃状态
	public static final String FinishedGoodType = "cn.tianma.FinishedGood";// 成品
	public static final String SemiFinishedGoodType = "cn.tianma.SemiFinishedGood";// 半成品
	public static final String RawMaterial = "cn.tianma.RawMaterial";// 原材料

	public static boolean refreshParts() {

		return false;
	}

	public static boolean refreshPartsToERP() {
		System.out.println("SLC ==开始为发送ERP查询部件==>");
		List<WTPart> list = new ArrayList<WTPart>();

		try {
			QuerySpec qs = new QuerySpec(WTPart.class);
			qs.setAdvancedQueryEnabled(true);
			View view = ViewHelper.service.getView("Design");

			// 增加过滤条件：iba属性存在值
			// 类型
			TypeDefinitionReference clientType = ClientTypedUtility.getTypeDefinitionReference(RawMaterial);
			SearchCondition sc = new SearchCondition(WTPart.class,
					Typed.TYPE_DEFINITION_REFERENCE + "." + TypeDefinitionReference.KEY + "."
							+ TypeDefinitionForeignKey.BRANCH_ID,
					SearchCondition.EQUAL, clientType.getKey().getBranchId());
			qs.appendWhere(sc, new int[] { 0 });
			qs.appendAnd();
			// 状态
			SearchCondition scState = new SearchCondition(WTPart.class, WTPart.LIFE_CYCLE_STATE,
					SearchCondition.NOT_EQUAL, discardState);
			qs.appendWhere(scState, new int[] { 0 });
			qs.appendAnd();
			// 视图
			qs.appendWhere(new SearchCondition(WTPart.class, "view.key.id", SearchCondition.NOT_EQUAL,
					view.getPersistInfo().getObjectIdentifier().getId()));
			 qs.appendAnd();
			// 最新的部件
			 qs.appendWhere(new SearchCondition(new KeywordExpression("A0.latestiterationinfo"), SearchCondition.EQUAL,new
			 KeywordExpression("1")));

			QueryResult qr = PersistenceHelper.manager.find(qs);
			System.out.println("SLC ===>>qs: " + qs);
			// 过滤出最新小版本的部件
			//qr = new LatestConfigSpec().process(qr);
			System.out.println("qr===>>>" + qr);
			System.out.println("SLC ====>>qr: " + qr.size());
			int count = 0;
			boolean bol=true;
			WTArrayList partList = new WTArrayList();
			while (qr.hasMoreElements()) {
				WTArrayList partList2 = new WTArrayList();
				boolean bol2=false;
				WTPart part = (WTPart) qr.nextElement();
				if (!isCheckOut(part)) {
					System.out.println("===>>>" + part.getNumber() + " | " + part.getViewName());
					partList.add(part);
					partList2.add(part);
					// 调用发送ERP接口
					bol2 = ext.generic.erp.service.PartReleaseService.releasePersistable(partList2,
							ext.generic.erp.common.CommonUtil.getCurrentTime());
				}
				if(!bol2) {
					count++;
					System.out.println("SLC ==部件发布erp失败==>" + part.getNumber() + " | " + part.getViewName());
				}

			}
			
			System.out.println("SLC ==要发送erp的部件数==>>" + partList.size()+" |查到的部件: "+qr.size());
			System.out.println("SLC ==部件发布erp失败的个数==>"+count);

			return bol;
		} catch (RemoteException e) {
			e.printStackTrace();
		} catch (QueryException e) {
			e.printStackTrace();
		} catch (WTException e) {
			e.printStackTrace();
		}
		return false;

	}

	/**
	 * 判断该零件是否已经出库
	 * 
	 * @param part 零件对象
	 * @return true:出库<br>
	 *         false:未出库
	 */
	public static boolean isCheckOut(WTPart part) {
		boolean isCheckOut = false;
		if (part != null) {
			try {
				isCheckOut = WorkInProgressHelper.isCheckedOut(part);/* 获取零件的出库状态 */
			} catch (WTException e) {
				e.printStackTrace();
			}
		} // end if

		return isCheckOut;
	}
}
